﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace STAR.Spring.Framework.Module
{
    public class ProcessModule : IHttpModule
    {
        public void Dispose() { }

        SpringService service = new SpringService();

        public void Init(HttpApplication context)
        {
            //context.BeginRequest += new EventHandler(context_BeginRequest);
            context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);
        }

        void context_AuthenticateRequest(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            HttpApplication app = (HttpApplication)sender;
            ProcessRequest(app.Context);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            ProcessRequest(app.Context);
        }

        private void ProcessRequest(HttpContext httpContext)
        {
            if (httpContext.Request.Path.ToLower().Contains("springservice"))
            {
                string op = httpContext.Request["op"];
                string custom = httpContext.Request["custom"];
                bool isCustom = !string.IsNullOrEmpty(custom) && custom == "1";
                string result = string.Empty;
                if (!string.IsNullOrEmpty(op))
                {
                    if (op == "MultipleRequest")
                    {
                        RequestJsonParmInfos requestInfo = RequestJsonParmInfos.GetRequestJsonParmInfos();
                        if (requestInfo.IsParmRight)
                        {
                            result = service.GetMultipleRequest(requestInfo);
                        }
                        else
                        {
                            result = new ReturnInfo()
                                {
                                    returnValue = false,
                                    message = "参数不正确"
                                }.ToBaseResponse();
                        }

                        Log.LogWorker.logger.DebugFormat("多请求类型：参数{0}\r\n ",requestInfo.IsParmRight ? "正确" : "不正确");
                        if (requestInfo.Items!=null)
                        {
                            Log.LogWorker.logger.DebugFormat("请求数：{0}\r\n", requestInfo.Items.Length);
                            Log.LogWorker.logger.Debug(requestInfo.ToString());
                        }
                    }
                    else
                    {
                        RequestJsonParmInfo requestParms = new RequestJsonParmInfo();
                        if (requestParms.IsParmRight)
                        {
                            result = service.GetResultString(op, isCustom, requestParms);
                        }
                        else
                        {
                            result = new ReturnInfo()
                                {
                                    returnValue = false,
                                    message = "参数不正确"
                                }.ToBaseResponse();
                        }

                        Log.LogWorker.logger.DebugFormat("请求类型：{0}，请求名称 {1}，参数{2} ", op, requestParms.RequestName, requestParms.IsParmRight ? "正确" : "不正确");
                        if (requestParms.Parms != null)
                        {
                            StringBuilder logTxt = new StringBuilder();
                            requestParms.Parms.ToList().ForEach(x => logTxt.AppendFormat("{0},", x));
                            Log.LogWorker.logger.DebugFormat("参数列表:{0}", logTxt.ToString().Trim(','));
                        }
                    }

                    if (!string.IsNullOrEmpty(result))
                    {
                        httpContext.Response.Clear();
                        httpContext.Response.Write(result);
                        httpContext.Response.End();
                    }
                }
            }
        }


    }
}
